<template>
  <div>
    <el-dialog
      width="600px"
      :title="$t('选择自派送批次')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form @submit.native.prevent ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item :label="$t('自派送批次')" prop="id">
          <el-select style="width: 100%" clearable filterable v-model="form.id">
            <el-option
              v-for="item in googleRouteBatchListStatus1List"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleSubmit" type="primary">{{ $t('确认') }}</el-button>
        <el-button @click="cancel">{{ $t('关闭') }}</el-button>
      </div>
    </el-dialog>
    <!-- 选择包裹导入 -->
    <SelectPackageImport
      :show.sync="selectPackageImport.show"
      :formData="selectPackageImport.formData"
      @getList="getList"
    />
  </div>
</template>

<script>
import SelectPackageImport from '@/views/orderManage/batchDispatch/components/selectPackageImport.vue'
import { googleRouteBatchListStatus1 } from '@/api/orderManage/batchDispatch'

export default {
  components: { SelectPackageImport },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      googleRouteBatchListStatus1List: [],
      /**选择包裹导入 */
      selectPackageImport: {
        show: false,
        formData: {},
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
        googleRouteBatchListStatus1().then((res) => {
          this.googleRouteBatchListStatus1List = res.rows
        })
      }
    },
  },
  methods: {
    getList() {
      this.$emit('getList')
      this.cancel()
    },

    handleSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const formData = this.googleRouteBatchListStatus1List.find((i) => i.id == this.form.id)
          this.selectPackageImport.formData = formData
          this.selectPackageImport.show = true
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
